package org.come.login;

import org.come.Frame.GameJframe;
import org.come.action.MapAction;
import org.come.bean.ConfigureBean;
import org.come.bean.LoginRoleInfo;
import org.come.bean.LoginUserBean;
import org.come.entity.RegionResult;
import org.come.model.Configure;
import org.come.socket.Agreement;
import org.come.socket.DownLoadTxt;
import org.come.socket.GameClient;
import org.come.socket.SendMessageUntil;
import org.come.test.Main;
import org.come.until.GsonUtil;
import org.come.until.UserMessUntil;
import org.come.until.Util;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.math.BigDecimal;
import java.util.Map;

public class AreaMouslisten implements MouseListener {
	private int caozuo;
	private BigDecimal i;
	private SpriteBtn btn;
	private LoginJpanel loginJpanel;
	private LoginRoleInfo roleInfo;
    private RegionResult area;

    public AreaMouslisten(BigDecimal i, SpriteBtn btn, LoginJpanel loginJpanel, RegionResult area) {
        super();
        this.i = i;
        this.btn = btn;
        this.loginJpanel = loginJpanel;
        this.area = area;
    }

	public AreaMouslisten(SpriteBtn btn, LoginJpanel loginJpanel, LoginRoleInfo roleInfo, int type) {
		super();
		this.btn = btn;
		this.loginJpanel = loginJpanel;
		this.roleInfo = roleInfo;
		this.caozuo = type;
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub

	}
	@Override
	public void mousePressed(MouseEvent e) {
		// 点击
		if (btn != null) {
			if (btn.isChoose()) {
				if (btn.getZhen() != 2) {// 选择类 选中
					btn.btn(2);
				}
			} else {
				btn.btn(2);
			}
		}
	}
	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		// 鼠标释放
		if (btn != null) {
			if (!btn.isChoose()) {// 非选择类生效
				btn.btn(0);
				dianJi();
			}
		} else {
			dianJi();
		}
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		if (btn != null) {
			if (btn.getZhen() != 2) {
				btn.btn(1);
			} else {
				btn.btn(0);
			}
		}
	}
	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		if (btn != null) {
			if (btn.getZhen() != 2) {
				btn.btn(0);
			} else {
				btn.btn(1);
			}
		}
	}

	private void dianJi() {
		try {
			/** zrikka 2020 0414 **/
			if (area != null) {
				Main.frame.getLoginJpanel().getAreaView().getLabMsgTip().setText("");
				GameClient.BT = area.getRA_NAME();
				GameJframe.string= area.getRA_NAME();
				Main.frame.getLoginJpanel().setRa_id(area.getRA_ID()+"");
				GameClient.tempreId=area.getRA_ID();
				Main.frame.getLoginJpanel().framechange(0, null, null);
				SendMessageUntil.getClientUntil(area.getIp(),area.getPort());
				DownLoadTxt.ip=area.getIp();//txtIp
				DownLoadTxt.port=Integer.parseInt(area.getDowport());//txtport

				// 常用角色-添加区名称
				Main.frame.getRoleInfo().setAreaId(area.getRA_ID());
				Main.frame.getRoleInfo().setAreaName(area.getRA_NAME());
			} else {
				if (caozuo == 0) {
					// 建立连接
					try {
						RegionResult area = Util.getRegionResultByName(roleInfo.getAreaName());
						if (area != null) {
							Main.frame.getLoginJpanel().getAreaView().getLabMsgTip().setText("");
							GameClient.BT = area.getRA_NAME();
							Main.frame.getLoginJpanel().setRa_id(area.getRA_ID() + "");
							GameClient.tempreId = area.getRA_ID();
							SendMessageUntil.getClientUntil(area.getIp(), area.getPort());
							DownLoadTxt.ip = area.getIp();
							DownLoadTxt.port = Integer.parseInt(area.getDowport());
							Main.frame.getRoleInfo().setAtId(area.getRA_ID().toString());
						}
					} catch (Exception e1) {
						e1.printStackTrace();
					}
					Main.frame.setRoleInfo(roleInfo);

					RoleLoginThread roleLoginThread = new RoleLoginThread(this.roleInfo);
					Thread t1 = new Thread(roleLoginThread);
					t1.start();


					ConfigureBean allConfigure = UserMessUntil.getConfigureBean();
					Map<BigDecimal, Configure> item = allConfigure.getAllConfigure();
					Configure configure = item.get(new BigDecimal(1));
					Util.zzs = Integer.parseInt(configure.getLzjskg());
				} else {
					loginJpanel.getAreaView().delCommonlyUsedRoles(loginJpanel, roleInfo);
				}
			}
			/** zrikka 2020 0415 */
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	private final class RoleLoginThread implements Runnable{
		private LoginRoleInfo roleInfo;
		public RoleLoginThread(LoginRoleInfo loginRoleInfo) {
			this.roleInfo = loginRoleInfo;
			Main.frame.setRoleInfo(loginRoleInfo);
		}
		@Override
		public void run() {
			while (true){
				Boolean is = MapAction.flagAction.get(SendMessageUntil.gameServerKey);
				if (is != null && is) {
					// 登录游戏
					LoginUserBean loginUserBean = new LoginUserBean();
					loginUserBean.setUsername(this.roleInfo.getAccount());// 设置用户名
					loginUserBean.setPassword(this.roleInfo.getPassword());// 设置密码
					roleInfo.setAccount(this.roleInfo.getAccount());
					roleInfo.setPassword(this.roleInfo.getPassword());
					// 向服务器发送信息
					String mesage = Agreement.getAgreement().LoginAgreement(GsonUtil.getGsonUtil().getgson().toJson(loginUserBean));
					SendMessageUntil.toServer(mesage);
					loginJpanel.setRoleInfo(this.roleInfo);
					GameClient.username = this.roleInfo.getAccount();
					GameClient.userpasw = this.roleInfo.getPassword();
					break;
				}
			}
		}
	}
}
